package com.qyhl.party.party.center;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.facade.callback.NavCallback;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.gyf.immersionbar.ImmersionBar;
import com.makeramen.roundedimageview.RoundedImageView;
import com.qyhl.party.R;
import com.qyhl.party.R2;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.party.PartyLoginBean;
import com.qyhl.webtv.commonlib.entity.party.PartyUserInfo;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.weavey.loading.lib.LoadingLayout;

import butterknife.BindView;
import butterknife.OnClick;

@SuppressLint("NonConstantResourceId")
@Route(path = ARouterPathConstant.PARTY_USER_CENTER)
public class PartyDataActivity extends BaseActivity implements PartyDataContract.PartyDataView {
    @BindView(R2.id.header)
    RoundedImageView header;
    @BindView(R2.id.name)
    TextView name;
    @BindView(R2.id.community)
    TextView community;
    @BindView(R2.id.zone)
    TextView zone;
    @BindView(R2.id.face)
    TextView face;
    @BindView(R2.id.isWork)
    TextView isWork;
    @BindView(R2.id.study_count)
    TextView studyCount;
    @BindView(R2.id.score_count)
    TextView scoreCount;
    @BindView(R2.id.article_count)
    TextView articleCount;
    @BindView(R2.id.answer_count)
    TextView answerCount;
    @BindView(R2.id.challenge_count)
    TextView challengeCount;
    @BindView(R2.id.challenge_rank)
    TextView challengeRank;
    @BindView(R2.id.win_count)
    TextView winCount;
    @BindView(R2.id.lose_count)
    TextView loseCount;
    @BindView(R2.id.compare_rank)
    TextView compareRank;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;

    @Autowired(name = "token")
    String token;

    private PartyDataPresenter mPresenter;

    private PartyUserInfo mBean;

    private final static int REQUEST_CODE = 100;

    @Override
    protected int getContentView() {
        return R.layout.party_activity_center;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        mImmersionBar.statusBarColor(R.color.white).statusBarDarkFont(true).fitsSystemWindows(true).init();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        loadMask.setStatus(LoadingLayout.Loading);

        mPresenter = new PartyDataPresenter(this);

        refresh.setRefreshHeader(new MaterialHeader(this));
        refresh.setRefreshFooter(new ClassicsFooter(this));
        refresh.setEnableLoadMore(false);

        mPresenter.getUserInfo(token);
    }

    @Override
    protected void setListener() {
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                mPresenter.getUserInfo(token);
            }
        });

        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresenter.getUserInfo(token);
            }
        });
    }

    @OnClick({R2.id.back_btn, R2.id.change_data, R2.id.study_enter, R2.id.community_enter, R2.id.test_enter})
    public void onClick(View view) {
        int viewId = view.getId();
        if (viewId == R.id.back_btn) {
            finish();
        } else if (viewId == R.id.change_data) {
            ARouter.getInstance()
                    .build(ARouterPathConstant.PARTY_SUPPLEMENT)
                    .withBoolean("isEdit", true)
                    .withString("token", token)
                    .withSerializable("bean", mBean)
                    .navigation(this, REQUEST_CODE);
        } else if (viewId == R.id.study_enter) {
            ARouter.getInstance()
                    .build(ARouterPathConstant.PARTY_BUILDING_HOME)
                    .withInt("position", 0)
                    .navigation(this, new NavCallback() {
                        @Override
                        public void onArrival(Postcard postcard) {
                            finish();
                        }
                    });
        } else if (viewId == R.id.community_enter) {
            ARouter.getInstance()
                    .build(ARouterPathConstant.PARTY_BUILDING_HOME)
                    .withInt("position", 2)
                    .navigation(this, new NavCallback() {
                        @Override
                        public void onArrival(Postcard postcard) {
                            finish();
                        }
                    });
        } else if (viewId == R.id.test_enter) {
            ARouter.getInstance()
                    .build(ARouterPathConstant.PARTY_BUILDING_HOME)
                    .withInt("position", 1)
                    .navigation(this, new NavCallback() {
                        @Override
                        public void onArrival(Postcard postcard) {
                            finish();
                        }
                    });
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void setInfo(PartyUserInfo bean) {
        loadMask.setReloadButtonText("点击重试~");
        loadMask.setStatus(LoadingLayout.Success);
        refresh.finishRefresh();

        mBean = bean;

        name.setText(bean.getName());

        Glide.with(this)
                .load(CommonUtils.getInstance().getUserLogo())
                .apply(new RequestOptions().error(R.drawable.comment_head_default).placeholder(R.drawable.comment_head_default))
                .into(header);

        if (bean.getDep() != null) {
            community.setVisibility(View.VISIBLE);
            isWork.setVisibility(View.VISIBLE);
            community.setText("单位：" + bean.getDep().getName());

            if (bean.getIsWork() == 0) {
                isWork.setText("在职情况：非在职");
            } else {
                isWork.setText("在职情况：在职");
            }
        } else {
            community.setVisibility(View.GONE);
            isWork.setVisibility(View.GONE);
        }

        zone.setText("所在区域：" + bean.getZone().getName());

        if (bean.getPolFace().equals("PARTY_MEMBER")) {
            face.setText("政治面貌：党员");
        } else if (bean.getPolFace().equals("LEAGUE_MEMBER")) {
            face.setText("政治面貌：团员");
        } else {
            face.setText("政治面貌：群众");
        }

        studyCount.setText(bean.getStudyCount() + "");

        scoreCount.setText(bean.getIntegral() + "");

        articleCount.setText(bean.getTopicCount() + "");

        answerCount.setText(bean.getRecordCount() + "");

        challengeCount.setText(bean.getBestPassCount() + "");

        challengeRank.setText("地区排名：" + bean.getPassTopNum());

        winCount.setText(bean.getPkCorrectCount() + "");

        loseCount.setText(bean.getPkWrongCount() + "");

        compareRank.setText("地区排名：" + bean.getPkTopNum());


    }

    @Override
    public void setError(String msg) {
        if (msg.contains("403")) {
            mPresenter.isLogin();
            loadMask.setErrorText("登录授权失败，请重新授权！");
        } else {
            loadMask.setErrorText(msg);
        }
        loadMask.setReloadButtonText("点击重试~");
        loadMask.setStatus(LoadingLayout.Error);
        refresh.finishRefresh();
    }

    @Override
    public void LoginSuccess(PartyLoginBean bean) {
        token = bean.getAuthorization();
    }

    @Override
    public void LoginError(String msg) {
        showToast(msg);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
            mPresenter.getUserInfo(token);
        }
    }
}
